package com.leyou.cart.web;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.impl.CartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CartController
 * @Date 2019/2/27 9:07
 */
@RestController
public class CartController {

    @Autowired
    private CartServiceImpl cartService;

    /**
     * 添加商品到购物车
     *
     * @param cart
     * @return
     */
    @PostMapping()
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询用户购物车
     *
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Cart>> queryCarts() {
        return ResponseEntity.ok(cartService.queryCarts());
    }

    /**
     * 修改购物车商品数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @PutMapping()
    public ResponseEntity<Void> updateNum(
            @RequestParam("id") Long skuId,
            @RequestParam("num") Integer num
    ) {
        cartService.updateNum(skuId, num);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除购物车的特定购物项
     *
     * @param skuId
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteCart(@PathVariable("id") String skuId) {
        cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加本地购物车到redis
     * @return
     */
    @PostMapping("insert")
    public ResponseEntity<Void> insertLocalCarts(@RequestBody List<Cart> carts) {
        cartService.insertLocalCarts(carts);
        return ResponseEntity.ok().build();
    }
}
